package com.suntimes.guangjiegou.component;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;

import com.suntimes.guangjiegou.R;

public class TabBar extends LinearLayout implements OnClickListener {
	
	private static final String TAG = "TabBar";

	private TabBarItem mTabItem1;
	private TabBarItem mTabItem2;
	private TabBarItem mTabItem3;
	private TabBarItem mTabItem4;
	private TabBarItem mTabItem5;
	private TabBarItem mSelectedTab;
	
	private OnClickTabItemListener mListener;
	
	public TabBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.view_tabbar, this);
		mTabItem1 = (TabBarItem) findViewById(R.id.item_tabbar_item1);
		mTabItem2 = (TabBarItem) findViewById(R.id.item_tabbar_item2);
		mTabItem3 = (TabBarItem) findViewById(R.id.item_tabbar_item3);
		mTabItem4 = (TabBarItem) findViewById(R.id.item_tabbar_item4);
		mTabItem5 = (TabBarItem) findViewById(R.id.item_tabbar_item5);
		
		mTabItem1.setTabIndex(0);
		mTabItem2.setTabIndex(1);
		mTabItem3.setTabIndex(2);
		mTabItem4.setTabIndex(3);
		mTabItem5.setTabIndex(4);
		
		mTabItem1.setText(R.string.tab_renmen);
		mTabItem2.setText(R.string.tab_tapei);
		mTabItem3.setText(R.string.tab_guangjie);
		mTabItem4.setText(R.string.tab_shoucang);
		mTabItem5.setText(R.string.tab_shezhi);
		
		mTabItem1.setDrawable(R.drawable.tab_icon_hot);
		mTabItem2.setDrawable(R.drawable.tab_icon_hot);
		mTabItem3.setDrawable(R.drawable.tab_icon_shopping);
		mTabItem4.setDrawable(R.drawable.tab_icon_hot);
		mTabItem5.setDrawable(R.drawable.tab_icon_hot);
		
		setupListeners();
	}
	
	public void setupListeners() {
		mTabItem1.setOnClickListener(this);
		mTabItem2.setOnClickListener(this);
		mTabItem3.setOnClickListener(this);
		mTabItem4.setOnClickListener(this);
		mTabItem5.setOnClickListener(this);
		
	}
	
	@Override
	public void onClick(View v) {
		if(v instanceof TabBarItem) {
			if(mSelectedTab != null) {
				mSelectedTab.setSelected(false);
			}
			TabBarItem item = (TabBarItem) v;
			mSelectedTab = item;
			mSelectedTab.setSelected(true);
			if(mListener != null) {
				mListener.onClickTabItem(item);
			}
		}
	}
	
	public void setSelectedTab(TabBarItem tabItem) {
		onClick(tabItem);
	}
	
	public void selectTab(int index) {
		if(mSelectedTab != null) {
			mSelectedTab.setSelected(false);
		}
		switch (index) {
		case 0:
			mSelectedTab = mTabItem1;
			break;
		case 1:
			mSelectedTab = mTabItem2;
			break;
		case 2:
			mSelectedTab = mTabItem3;
			break;
		case 3:
			mSelectedTab = mTabItem4;
			break;
		case 4:
			mSelectedTab = mTabItem5;
			break;
		}
		mSelectedTab.setSelected(true);
		
	}
	
	public void clearSelected() {
		mTabItem1.setSelected(false);
		mTabItem2.setSelected(false);
		mTabItem3.setSelected(false);
		mTabItem4.setSelected(false);
		mTabItem5.setSelected(false);
	}
	
	public TabBarItem getSelectedTab() {
		return mSelectedTab;
	}
	
	public void updateByLanguageChanged() {
		
	}
	
	public void release() {
		mListener = null;
	}

	public void setOnClickTabItemListener(OnClickTabItemListener listener) {
		mListener = listener;
	}
	
	public static interface OnClickTabItemListener {
		void onClickTabItem(TabBarItem tabItem);
	}

}
